package com.chen.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.chen.pojo.Bill;
import com.chen.pojo.Provider;
import com.chen.service.bill.BillService;
import com.chen.service.provider.ProviderService;
import com.chen.tools.Constants;
import com.chen.tools.PageSupport;

@Controller
@RequestMapping(value="/bill")
public class BillController {
	
	private Logger logger = Logger.getLogger(BillController.class);
	
	@Resource(name="billService")
	private BillService billService;
	
	
	@Resource(name="providerService")
	private ProviderService providerService;
	/**
	 * ��������ѯ���ж�����Ϣ����ҳ
	 * @param model
	 * @param productName
	 * @param providerId
	 * @param isPayment
	 * @param pageIndex
	 * @return
	 */
	@RequestMapping(value="/bills")
	public String getBillList(Model model,
			@RequestParam(value="productName",required=false) String productName,
			@RequestParam(value="providerId",required=false) String providerId,
			@RequestParam(value="isPayment",required=false) String isPayment,
			@RequestParam(value="pageIndex",required=false) String pageIndex){
		List<Bill> billList = new ArrayList<Bill>();
		Integer _queryProviderId = 0;
		Integer queryIsPayment = 0;
		//����ҳ������
		int pageSize = Constants.pageSize;
		// ��ǰҳ��
		int currentPageNo = 1;
		if (productName == null) {
			productName = "";
			logger.debug("productName:"+productName);
		}
		if (providerId != null && !providerId.equals("")) {
			_queryProviderId = Integer.parseInt(providerId);
			logger.debug("_queryProviderId:"+_queryProviderId);
		}
		if (isPayment != null && !isPayment.equals("")) {
			queryIsPayment = Integer.parseInt(isPayment);
			logger.debug("queryIsPayment:"+queryIsPayment);
		}
		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
				logger.debug("pageIndex:"+pageIndex);
			} catch (NumberFormatException e) {
				return "redirect:/user/syserror.html";
			}
		}
		// ������(��)
		int totalCount = 0;
		try {
			totalCount = billService.getBillCountList(productName, _queryProviderId.toString(), queryIsPayment.toString());
			logger.debug("totalCount:"+totalCount);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// ��ҳ��
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);
		int totalPageCount = pages.getTotalPageCount();
		// ������ҳ��βҳ
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}
		try {
			billList = billService.getBillList(productName, _queryProviderId.toString(), queryIsPayment.toString(), currentPageNo, pageSize);
			for (Bill bill : billList) {
				logger.debug("bill:"+bill.getProviderName());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<Provider> providerList = new ArrayList<Provider>();
		try {
			providerList = providerService.queryList();
			for (Provider provider : providerList) {
				System.out.println(provider.getProAddress());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("providerList", providerList);
		model.addAttribute("billList", billList);
		model.addAttribute("productName", productName);
		model.addAttribute("providerId", providerId);
		model.addAttribute("isPayment", isPayment);
		model.addAttribute("totalPageCount", totalPageCount);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("currentPageNo", currentPageNo);
		return "jsp/billlist";
	}
	/**
	 * ����Ӧ�̵���Ϣ��ѯ������ʹ��json���䷵��ҳ��
	 * @return
	 */
	@RequestMapping(value="toadd",method=RequestMethod.GET,produces={"application/json;charset=UTF-8"})
	@ResponseBody
	public List<Provider> toAdd(){
		List<Provider> providerList = null;
		try {
			providerList = providerService.queryList();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return providerList;
	}
	/**
	 * ��������ҳ��
	 * @return
	 */
	@RequestMapping(value="toaddgo",method=RequestMethod.GET)
	public String togoadd(){
		return "jsp/billadd";
	}
	/**
	 * �����Ʒ��Ϣ
	 * @param model
	 * @param bill
	 * @return
	 */
	@RequestMapping(value="billadd")
	public String addBill(Model model,Bill bill){
		boolean flag = false;
		try {
			bill.setCreationDate(new Date());
			flag = billService.add(bill);
			if(flag) {
				return "redirect:/bill/bills";
			} else {
				return "jsp/billadd";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "null";
	}
	/**
	 * �鿴������Ʒ��Ϣ
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/view/{id}", method = RequestMethod.GET)
	public String view(@PathVariable String id,Model model){
		Bill bill = new Bill();
		try {
			bill = billService.getBillById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("bill", bill);
		return "jsp/billview";
	}
	/**
	 * ʹ��rest�������޸�ҳ��
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/viewupdate/{id}",method= RequestMethod.GET)
	public String updView(@PathVariable String id,Model model){
		Bill bill = new Bill();
		try {
			bill = billService.getBillById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("bill", bill);
		return "jsp/billmodify";
	}
	@RequestMapping(value="/updatebill")
	public String updateBill(Bill bill){
		boolean flag = false;
		bill.setCreationDate(new Date());
		try {
			flag = billService.modify(bill);
			if(flag){
				return "redirect:/bill/bills";
			} else {
				return "jsp/billmodify";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@RequestMapping(value="/deletebill",method=RequestMethod.GET)
	@ResponseBody
	public Object deleteBill(@RequestParam String billid){
		boolean flag = false;
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			flag = billService.deleteBillById(billid);
			if(flag) {
				map.put("delResult", "true");
				logger.debug(flag);
			} else {
				map.put("delResult", "false");
				logger.debug(flag);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return JSONArray.toJSONString(map);
	}
}
